package com.sgxy.controller;

import com.sgxy.entity.UserInf;
import com.sgxy.service.UserInfService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Controller
public class LoginController {
    private final UserInfService userInfService;
    @Autowired
    public LoginController(UserInfService userInfService) {
        this.userInfService = userInfService;
    }
    @RequestMapping("/login")
    public ModelAndView loginAction(String loginname, String password){
        ModelAndView modelAndView = new ModelAndView();
        Example example = new Example(UserInf.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginName",loginname);
        criteria.andEqualTo("password",password);
        UserInf userInf = userInfService.queryUserInfByloginName(example);
        if(userInf!=null){
            modelAndView.addObject("userInf",userInf);
            modelAndView.setViewName("main.jsp");
        }else{
            modelAndView.addObject("loginFail","账号不存在或密码错误");
            modelAndView.setViewName("loginForm.jsp");
        }
        return modelAndView;
    }
}
